﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class DownloadAdd : PowerBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                InitControl();
            }
                string FileExt = LTP.Common.ConfigHelper.GetConfigString("DownloadExt");
                int FileSize = Convert.ToInt32(LTP.Common.ConfigHelper.GetConfigString("DownloadSize")) / (1024 * 1024);
                string[] tempExt = FileExt.Split(',');
                FileExt = string.Empty;
                foreach (string ext in tempExt)
                {
                    FileExt += "*" + ext + ";";
                }
                FileExt = FileExt.Substring(0, FileExt.Length - 1);
                Response.Write(string.Format("<script>FileExt='{0}';FileSize={1}</script>", FileExt, FileSize));
                LblSize.Text = FileSize.ToString();
          
            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSave, "checkform()", false);
        }
        protected void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"]))
            {

                CJJCH.Model.T_NET_MenuSecond model = new Model.T_NET_MenuSecond();
                model = (new CJJCH.BLL.T_NET_MenuSecond()).GetModel(Convert.ToInt32(Request.QueryString["secondid"]));
                if (model != null)
                {
                    lblSecondId.Text = LTP.Common.StringClass.EncodeString(model.ID.ToString());
                    DownloadTop1.SecondId = Convert.ToInt32(lblSecondId.Text);
                    lblSecondName.Text = LTP.Common.StringClass.EncodeString(model.MenuName);
                    DownloadTop1.SecondName = LTP.Common.StringClass.EncodeString(lblSecondName.Text);
                    lblFirstID.Text = LTP.Common.StringClass.EncodeString(model.FirstID.ToString());
                    if (model.HaveChild == true)
                    {
                        DownloadTop1.TypeID = 1;
                        IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
                        list = (new CJJCH.BLL.T_NET_MenuThird()).GetDrop(Convert.ToInt32(lblSecondId.Text));
                        int typeCount = list.Count;
                        if (typeCount > 0)
                        {
                            for (int i = 0; i < typeCount; i++)
                            {
                                dropType.Items.Add(new ListItem(list[i].MenuName, list[i].ID.ToString()));
                            }
                            list = null;
                        }
                    }
                    else
                    {
                        DownloadTop1.TypeID = 0;
                        type.Style.Add(" display", "none");
                    }
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }                
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string error = checkInput();
            if(error=="")
            {
                CJJCH.Model.T_NET_Download model = new Model.T_NET_Download();
                model.ID = Guid.NewGuid();
                model.DataTitle =LTP.Common.StringClass.EncodeStr( txtDataTitle.Text);
                model.DataNote = LTP.Common.StringClass.RemoveSQL(txtNote.Value);
                model.DataUrl = hdSaveFilePath.Value;
                model.OrderNum = Convert.ToInt32(txtTrans.Text.Trim());
                model.PlusDate = Convert.ToDateTime(txtPlusDate.Text);
                model.BuildDate = DateTime.Now;
                model.Hits = 0;
                model.ManagerID = SessionManagerID;
                model.SecondID = Convert.ToInt32(lblSecondId.Text);
                model.FirstID = Convert.ToInt32(lblFirstID.Text);
                if (dropType.SelectedValue != "")
                {
                    model.ThirdID = Convert.ToInt32(dropType.SelectedValue);
                }
                else 
                {
                    model.ThirdID = 0;
                }
                error = (new CJJCH.BLL.T_NET_Download()).AddDownload(model);
                if (error == "")
                {
                    if (chkAdd.Checked == true)
                    {
                        LTP.Common.WindowClass.WindowGoHref("添加成功！", "DownloadAdd.aspx?secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim()));
                    }
                    else 
                    {
                        LTP.Common.WindowClass.WindowGoHref("添加成功！", "DownloadList.aspx?secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim()));
                    }
                }
                else 
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
        }

        protected string checkInput()
        {
            StringBuilder error = new StringBuilder();
            if (this.txtDataTitle.Text.Trim() == "")
            {
                error.Append("请输入标题！ \\n");
            }
            //if (this.txtNote.Value== "")
            //{
            //    error.Append("请输入内容！ \\n");
            //}
            if (this.txtPlusDate.Text.Trim() == "")
            {
                error.Append("请输入发布日期	！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDateTime(txtPlusDate.Text))
            {
                error.Append("请输入发布的日期！\\n");
            }
            if (hdSaveFilePath.Value == "") 
            {
                error.Append("请上传文件!\\n");
            }
            if (this.txtTrans.Text == "")
            {
                error.Append("请输入排序号！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(this.txtTrans.Text.Trim()))
            {
                error.Append("请输入正确的排序号！ \\n");
            }
            if (type.Style["display"] != "none")
            {
                if (this.dropType.SelectedValue == "")
                {
                    error.Append("请选择所属类型！ \\n");
                }
            }
            return error.ToString();
        }
    }
}